package entidade;

import exceptions.ReturnWsException;
import java.util.Stack;
import java.util.Vector;
import mobcatservice.MobCatServiceService;
import mobcatservice.MobCatServiceService_Stub;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author victorinox
 */
public class MobCatController {

    private MobCatServiceService service = new MobCatServiceService_Stub();
    private static MobCatController instance = new MobCatController();

    public static MobCatController getInstance() {
        return instance;
    }

    private MobCatController() {
    }

    public Vector listEstados() throws ReturnWsException {
        Vector estados = new Stack();
        try {
            String[] str = service.listEstados();
            for (int i = 0; i < str.length; i++) {
                estados.addElement(new Estado(str[i]));
            }
        } catch (Exception ex) {
            throw new ReturnWsException(ex.toString());
        }
        return estados;
    }

    public Vector listCidades(int estadoId) throws ReturnWsException {
        Vector cidades = new Stack();
        try {
            String[] str = service.listCidadesPorEstado(String.valueOf(estadoId));
            for (int i = 0; i < str.length; i++) {
                cidades.addElement(new Cidade(str[i]));
            }
        } catch (Exception ex) {
            throw new ReturnWsException(ex.getMessage());
        }
        return cidades;
    }

    public Vector listCategoriaRaiz(int cidadeId) throws ReturnWsException {
        Vector categorias = new Stack();
        try {
            String[] str = service.listCategoriasRaizPorCidade(String.valueOf(cidadeId));
            for (int i = 0; i < str.length; i++) {
                categorias.addElement(new Categoria(str[i]));
            }
        } catch (Exception ex) {
            throw new ReturnWsException(ex.getMessage());
        }
        return categorias;
    }

    public Vector listSubCategoria(int categoriaId) throws ReturnWsException {
        Vector categorias = new Stack();
        try {
            String[] str = service.listSubCategorias(String.valueOf(categoriaId));
            for (int i = 0; i < str.length; i++) {
                categorias.addElement(new Categoria(str[i]));
            }
        } catch (Exception ex) {
            throw new ReturnWsException(ex.getMessage());
        }
        return categorias;
    }

    public Vector listRegistros(int categoriaId) throws ReturnWsException {
        Vector registros = new Stack();
        try {
            String[] str = service.listRegistrosPorCategoria(String.valueOf(categoriaId));
            for (int i = 0; i < str.length; i++) {
                registros.addElement(new Registro(str[i]));
            }
        } catch (Exception ex) {
            throw new ReturnWsException(ex.getMessage());
        }
        return registros;
    }

    public Vector buscarRegistros(String palavraChave, int cidadeId) throws ReturnWsException {
        Vector registros = new Stack();
        try {
            String[] str = service.buscarRegistros(palavraChave, cidadeId);
            for (int i = 0; i < str.length; i++) {
                registros.addElement(new Registro(str[i]));
            }
        } catch (Exception ex) {
            throw new ReturnWsException(ex.getMessage());
        }
        return registros;
    }

    public Registro getRegistro(int registroId) throws ReturnWsException {
        try {
            String[] str = service.getRegistro(String.valueOf(registroId));
            if (str.length >= 1) {
                return new Registro(str[0]);
            }
        } catch (Exception ex) {
            throw new ReturnWsException(ex.getMessage());
        }
        return null;
    }

    public Categoria getCategoria(int categoriaId) throws ReturnWsException {
        try {
            String str = service.getCategoria(String.valueOf(categoriaId));
            return new Categoria(str);
        } catch (Exception ex) {
            throw new ReturnWsException(ex.getMessage());
        }
    }
}
